﻿var ContentApprovalStatus = "";
var _listId = "";

//used to show approve/reject dialog
function showApproveAll_BulkApproval(siteUrl) {

    var pageurl = siteUrl + "/SitePages/ApproveRejectItems.aspx";
    var options = {
        url: pageurl,
        dialogReturnValueCallback:
        function (dialogResult, returnValue) {
            if (dialogResult == 1) {
                changeItemStatus(returnValue);
            }
            else {
                window.location.href = window.location.href;
            }

        }

    };

    SP.UI.ModalDialog.showModalDialog(options);

}

//used to determine whether the 'approve/reject selection' 
//ribbon will be enalbed or disabled
function enableApprovalAll_BulkApproval() {
    var ctx = SP.ClientContext.get_current();

    if (ContentApprovalStatus == "" || _listId != SP.ListOperation.Selection.getSelectedList()) 
    {
        var listId = SP.ListOperation.Selection.getSelectedList();
        var currentLib = ctx.get_web().get_lists().getById(listId);
        ctx.load(currentLib);

        ctx.executeQueryAsync(function () {
            ContentApprovalStatus = currentLib.get_enableModeration().toString();
            _listId = SP.ListOperation.Selection.getSelectedList();
            RefreshCommandUI();
        },
            function () {
                return false;
            });
    }
    else 
    {
        if (ContentApprovalStatus == "true") 
        {
            return SP.ListOperation.Selection.getSelectedItems(ctx).length >= 1;
        }
        else 
        {
            return false;
        }
    }
}

function changeItemStatus(returnValue) {

    //break return value for moderation status and comments
    var moderation = returnValue.split("$");
    var moderationStatus = moderation[0];
    cnt = moderation[0].length;
    var moderationcomment = returnValue.substring(cnt+1);

    var ctx = SP.ClientContext.get_current();
    //get current list 
    web = ctx.get_web();
    var listId = SP.ListOperation.Selection.getSelectedList();
    var currentLib = web.get_lists().getById(listId);
    
    //get all selected list items
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);

    for (var i = 0; i < selectedItems.length; i++) {
        var currentItem = currentLib.getItemById(selectedItems[i].id);
        ctx.load(currentItem);

        //set moderation status according to returned value
        if (moderationStatus == "Approved")
            currentItem.set_item('_ModerationStatus', 0);
        else if (moderationStatus == "Denied")
            currentItem.set_item('_ModerationStatus', 1);
        else if(moderationStatus == "Pending")
            currentItem.set_item('_ModerationStatus', 2);

        currentItem.set_item('_ModerationComments', moderationcomment);
        currentItem.update();
    }

    ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded_BulkApproval)
        , Function.createDelegate(this, this.onQueryFailed_BulkApproval));
}
function onQuerySucceeded_BulkApproval(sender, args) {
            window.location.href = window.location.href;
}
function onQueryFailed_BulkApproval(sender, args) {
    alert(args.get_message());
}

